﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using sharp_net;
using sharp_net.Repositories;
using sharp_net.Specifications;
using zkdao.Domain;
using zkdao.Enum;

namespace zkdao.Application {

    public class RebateApplication : BaseApplication, IRebateService {

        public RebateData Get(Guid ID) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var rebateRepository = context.GetRepository<Rebate>();
                var rebate = rebateRepository.GetById(ID);
                if (rebate == null)
                    return null;
                var rebateData = Mapper.Map<Rebate, RebateData>(rebate);
                return rebateData;
            }
        }

        public IEnumerable<RebateData> GetListPager(ref int count, int pageSize = 10, int pageIndex = 1) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var rebateRepository = context.GetRepository<Rebate>();
                var rebates = rebateRepository.FindAll(Specification<Rebate>.Eval(r => r.StartTime <= DateTime.Now || r.EndTime >= DateTime.Now), pageIndex, pageSize);
                if (rebates == null || rebates.Count() == 0)
                    return null;
                count = rebateRepository.FindAll().Count();
                var rebateDatas = new List<RebateData>();
                foreach (var rebate in rebates) {
                    rebateDatas.Add(Mapper.Map<Rebate, RebateData>(rebate));
                }
                return rebateDatas;
            }
        }

        public RebateData Submit(RebateData dataObject, bool force, string[] imageIDs, string[] tagNames) {
            if (dataObject == null)
                throw new ArgumentNullException("rebateDataObject");
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var rebateRepository = context.GetRepository<Rebate>();
                var imgRepository = context.GetRepository<Image>();
                var tagRepository = context.GetRepository<Tag>();

                Rebate rebate = Mapper.Map<RebateData, Rebate>(dataObject);
                rebate.Creat();
                rebate.ActEnum = force ? (int)eAct.Normal : (int)eAct.unApproved;

                int imageID = 0;
                if (imageIDs.Length > 0 && int.TryParse(imageIDs[0], out imageID)) {
                    var image = imgRepository.GetById(imageID);
                    if (image != null)
                        rebate.PhotoPath = image.Path;
                }

                rebate.Tags = new List<Tag>();
                var tags = tagRepository.FindAll(Specification<Tag>.Eval(c => tagNames.Contains(c.Name)));
                foreach (var tag in tags)
                    rebate.Tags.Add(tag);

                rebateRepository.Add(rebate);
                context.Commit();

                foreach (string id in imageIDs) {
                    var upImage = imgRepository.GetById(int.Parse(id));
                    upImage.HostID = rebate.Id.ToString();
                    upImage.HostType = eImageHostType.Rebate;
                    imgRepository.Update(upImage);
                }
                
                return Mapper.Map<Rebate, RebateData>(rebate);
            }
        }

        public void Approved(Guid ID) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var rebateRepository = context.GetRepository<Rebate>();
                var rebate = rebateRepository.GetById(ID);
                if (rebate == null)
                    throw new ArgumentNullException("rebateDataID");
                if (rebate.Act == eAct.unApproved) {
                    rebate.ActEnum = (int)eAct.Normal;
                    rebateRepository.Update(rebate);
                }
            }
        }

        public void Update(RebateData dataObject) {
            if (string.IsNullOrEmpty(dataObject.ID))
                throw new ArgumentNullException("ID");
            Rebate rebate = Mapper.Map<RebateData, Rebate>(dataObject);
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var rebateRepository = context.GetRepository<Rebate>();
                var uprebate = rebateRepository.Get(Specification<Rebate>.Eval(c => c.Id.ToString() == dataObject.ID));
                uprebate.Update(dataObject);
                rebateRepository.Update(uprebate);
            }
        }
    }
}